var _excluded = ['value', 'onChange', 'options', 'className'];
function _extends() {
  _extends = Object.assign
    ? Object.assign.bind()
    : function (target) {
        for (var i = 1; i < arguments.length; i++) {
          var source = arguments[i];
          for (var key in source) {
            if (Object.prototype.hasOwnProperty.call(source, key)) {
              target[key] = source[key];
            }
          }
        }
        return target;
      };
  return _extends.apply(this, arguments);
}
function _objectWithoutProperties(source, excluded) {
  if (source == null) return {};
  var target = _objectWithoutPropertiesLoose(source, excluded);
  var key, i;
  if (Object.getOwnPropertySymbols) {
    var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
    for (i = 0; i < sourceSymbolKeys.length; i++) {
      key = sourceSymbolKeys[i];
      if (excluded.indexOf(key) >= 0) continue;
      if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
      target[key] = source[key];
    }
  }
  return target;
}
function _objectWithoutPropertiesLoose(source, excluded) {
  if (source == null) return {};
  var target = {};
  var sourceKeys = Object.keys(source);
  var key, i;
  for (i = 0; i < sourceKeys.length; i++) {
    key = sourceKeys[i];
    if (excluded.indexOf(key) >= 0) continue;
    target[key] = source[key];
  }
  return target;
}
import { Checkbox as AntdCheckbox, Input } from 'antd';
import classnames from 'classnames';
import React, { forwardRef, useCallback, useRef } from 'react';
import { useFieldContext } from '../../_utils/contexts/FieldContext';
import useOther from '../../_utils/hooks/useOther';
import useRequestOptions from '../../_utils/hooks/useRequestOptions';
import createField from '../../createField';
import './index.less';
var CheckboxWithOther = /*#__PURE__*/ forwardRef(function (_ref, ref) {
  var value = _ref.value,
    onChange = _ref.onChange,
    optionsOrRequest = _ref.options,
    className = _ref.className,
    rest = _objectWithoutProperties(_ref, _excluded);
  var _useFieldContext = useFieldContext(),
    label = _useFieldContext.label;
  var _useRequestOptions = useRequestOptions(optionsOrRequest),
    options = _useRequestOptions.options;
  var otherRef = useRef('checkbox-other-' + Math.random());
  var _useOther = useOther({
      mode: 'multiple',
      value: value,
      onChange: onChange,
      options: options,
    }),
    otherChecked = _useOther.otherChecked,
    changeOtherChecked = _useOther.changeOtherChecked,
    selectedValue = _useOther.selectedValue,
    changeSelectedValue = _useOther.changeSelectedValue,
    otherValue = _useOther.otherValue,
    changeOtherValue = _useOther.changeOtherValue;
  var handleInputChange = useCallback(
    function (event) {
      changeOtherValue(event.target.value);
    },
    [changeOtherValue],
  );
  return /*#__PURE__*/ React.createElement(
    'span',
    {
      className: classnames('szzj-checkbox-with-other', className),
    },
    /*#__PURE__*/ React.createElement(
      AntdCheckbox.Group,
      _extends(
        {
          value: selectedValue,
          onChange: changeSelectedValue,
        },
        rest,
        {
          className: 'szzj-checkbox-with-other-checkbox',
          ref: ref,
        },
      ),
      options.map(function (item) {
        return /*#__PURE__*/ React.createElement(
          AntdCheckbox,
          {
            key: item.value,
            value: item.value,
          },
          item.label,
        );
      }),
    ),
    /*#__PURE__*/ React.createElement(
      AntdCheckbox,
      {
        value: otherRef.current,
        checked: otherChecked,
        className: 'szzj-checkbox-with-other',
        onChange: function onChange(event) {
          return changeOtherChecked(
            event === null || event === void 0 ? void 0 : event.target.checked,
          );
        },
      },
      '\u5176\u4ED6',
    ),
    otherChecked &&
      /*#__PURE__*/ React.createElement(Input, {
        placeholder: '\u8BF7\u8F93\u5165'.concat(label || ''),
        className: 'szzj-checkbox-with-other-input',
        maxLength: 100,
        value: otherValue,
        onChange: handleInputChange,
      }),
  );
});
export default createField(CheckboxWithOther);
